package com.atguigu.controller;

import com.atguigu.pojo.Department;
import com.atguigu.pojo.Result;
import com.atguigu.service.DepartmentService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("department")
public class DepartmentController {

    @Resource
    private DepartmentService departmentService;

    @PostMapping("add")
    public Result add(@RequestBody Department department) {
        departmentService.add(department);
        System.out.println("添加的数据是  "+department);
        return Result.success();
    }
    @PutMapping("update")
    public Result update(@RequestBody Department department) {
        System.out.println("---------------------");
        System.out.println(department);
        System.out.println("---------------------");
        departmentService.update(department);
        return Result.success();
    }

    @DeleteMapping("deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        departmentService.deleteById(id);
        return Result.success();
    }

    @DeleteMapping("deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        departmentService.deleteBatch(ids);
        return Result.success();
    }

    @GetMapping("selectAll")
    public Result selectAll(Department department) {
        List<Department> list = departmentService.selectAll(department);
        return Result.success(list);
    }

    @GetMapping("selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        return Result.success(departmentService.selectById(id));
    }

    @GetMapping("selectByOne")
    public Result selectByOne(@RequestParam Integer id) {
        return Result.success(departmentService.selectById(id));
    }

    @GetMapping("selectPage")
    public Result selectPage(Department department,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.success(departmentService.selectPage(department, pageNum, pageSize));
    }

}
